#include "mainwindow.h"
#include "ui_mainwindow.h"
#include<iostream>
#include<stdio.h>
#include<QDebug>
#include<QMimeData>
#include<QDesktopWidget>
#include<QClipboard>
#include<stdlib.h>
#include<QFiledialog>
#include<windows.h>
MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    ui->setupUi(this);
    pic.load("image/canvas.png");
    Qt::WindowFlags flags = 0;
        flags |= Qt::WindowMinimizeButtonHint;
        //setWindowFlags(flags); // 设置禁止最大化
        //setFixedSize(800,600); // 禁止改变窗口大小。
        resize( QSize( 800, 600 ));
        setMinimumSize(QSize( 800, 600 ));
        KEY_PRESSED=false;
        KEY_NUM=0;
        MOUSE_PRESSED=FALSE;
        MOUSE_MOVE=FALSE;
        MOUSE_RELEASED=FALSE;
        MOUSE_SHIFT=FALSE;
        BUFFER_FULL=FALSE;
        WHEEL_PRESSED=FALSE;
        RIGHT_PRESSED=FALSE;
        FILE_DRAGGED=FALSE;
        WINDOW_DRAGGED=FALSE;
        CTRL_ROOLUP=FALSE;
        CTRL_ROOLDOWN=FALSE;
        MOUSE_DOUBLECLICK=FALSE;
        TO_GET_FILENAME=FALSE;
        FULL=FALSE;
        MOUSE_X=0;
        MOUSE_Y=0;
        painter=NULL;
        board=NULL;
        ROOL_NUM=0;
        WINDOW_X=0;
        WINDOW_Y=0;
        this->setAttribute(Qt::WA_InputMethodEnabled, true);
        this->setAttribute(Qt::WA_KeyCompression, true);
        this->setWindowFlags(Qt::FramelessWindowHint);//去掉标题栏
        setWindowTitle(QString("TXTviewer by buptsoft"));

}

MainWindow::~MainWindow()
{
    delete ui;
}
void MainWindow::paintEvent(QPaintEvent *)
{
    painter=new QPainter(this);
    painter->drawPixmap(0,0,pic);
}
/*bool MainWindow::winEventFilter(MSG *message, long *result)
{
    printf("gg");
    //KEY_PRESSED=TRUE;
    //KEY_NUM=-2;
    MSG * m=(MSG *)message;
    switch(m->message){

    }
    //{
    //case WM_MBUTTONDOWN:
    //    qDebug()<<"OK";
    //    emit MBD();
    //default:
    //    break;
    //}
    return true;
}*/
bool MainWindow::nativeEvent(const QByteArray & eventType, void * message, long * result){
    if(board){
        QClipboard *clipboard =QApplication::clipboard();
        clipboard->setText(QString(QString::fromLocal8Bit(board)), QClipboard::Clipboard);
        free(board);
        board=NULL;
    }
    Q_UNUSED(eventType);
    MSG* m = reinterpret_cast<MSG*>(message);
    switch(m->message){
       case WM_KEYDOWN:
            if(m->wParam==VK_LEFT){
                KEY_PRESSED=TRUE;
                KEY_NUM=-3;
                return TRUE;
            }else if(m->wParam==VK_RIGHT){
                KEY_PRESSED=TRUE;
                KEY_NUM=-4;
                return TRUE;
            }else if(m->wParam==VK_RETURN){
                KEY_PRESSED=TRUE;
                KEY_NUM=2;
                return TRUE;
            }else if(m->wParam==VK_BACK){
                KEY_PRESSED=TRUE;
                KEY_NUM=-5;
                return TRUE;
            }else if(m->wParam==VK_DELETE){
                KEY_PRESSED=TRUE;
                KEY_NUM=-6;
                return TRUE;
            }else if((m->wParam==86) && (GetAsyncKeyState(VK_CONTROL)<0)){
                KEY_PRESSED=TRUE;
                KEY_NUM=-10;
                return TRUE;
            }else if((m->wParam==67) && (GetAsyncKeyState(VK_CONTROL)<0)){
                KEY_PRESSED=TRUE;
                KEY_NUM=-9;
                return TRUE;
            }else if((m->wParam==88) && (GetAsyncKeyState(VK_CONTROL)<0)){
                KEY_PRESSED=TRUE;
                KEY_NUM=-12;
                return TRUE;
            }else if((m->wParam==33) && (GetAsyncKeyState(VK_CONTROL)<0)){
                KEY_PRESSED=TRUE;
                KEY_NUM=-13;
                return TRUE;
            }else if((m->wParam==34) && (GetAsyncKeyState(VK_CONTROL)<0)){
                KEY_PRESSED=TRUE;
                KEY_NUM=-14;
                return TRUE;
            }else if(m->wParam==36){
                KEY_PRESSED=TRUE;
                KEY_NUM=-15;
                return TRUE;
            }else if(m->wParam==VK_TAB){
                KEY_PRESSED=TRUE;
                KEY_NUM=-16;
                return TRUE;
            }
            printf("%d",m->wParam);
       break;
       defult:return FALSE;
    }

    return FALSE;
}
void MainWindow::keyPressEvent(QKeyEvent * e)
{
    KEY_PRESSED=TRUE;
    if (e->modifiers() == (Qt::ControlModifier) && e->key() == Qt::Key_C){
       KEY_NUM=-9;
    }
    else if (e->modifiers() == (Qt::ControlModifier) && e->key() == Qt::Key_V){
       KEY_NUM=-10;
    }
    else if (e->modifiers() == (Qt::ControlModifier) && e->key() == Qt::Key_S){
       KEY_NUM=-11;
    }
    else if (e->modifiers() == (Qt::ControlModifier) && e->key() == Qt::Key_X){
       KEY_NUM=-12;
    }
    else{
        switch(e->key())
         {
         case Qt::Key_Left:KEY_NUM=-3;break;
         case Qt::Key_Up:KEY_NUM=-1;break;
         case Qt::Key_Right:KEY_NUM=-4;break;
         case Qt::Key_Down:KEY_NUM=-2;break;
         case Qt::Key_Backspace :KEY_NUM=-5;break;
         case Qt::Key_Delete :KEY_NUM=-6;break;
         case Qt::Key_Return :KEY_NUM=2;break;
         case Qt::Key_Enter  :KEY_NUM=2;break;
         case Qt::Key_PageUp:KEY_NUM=-13;break;
         case Qt::Key_PageDown:KEY_NUM=-14;break;
         case Qt::Key_Home:KEY_NUM=-15;break;
         default: break;
         }
    }
    //printf("++%d++",e->nativeScanCode());
}
void MainWindow::mousePressEvent(QMouseEvent *event){
    WINDOW_X=frameGeometry().topLeft().x();
    WINDOW_Y=frameGeometry().topLeft().y();
    GLOBAL_X=event->globalPos().x();
    GLOBAL_Y=event->globalPos().y();
    if (event->button() == Qt::LeftButton){
         if ( QApplication::keyboardModifiers () == Qt::ShiftModifier){
            MOUSE_SHIFT=TRUE;
         }else
            MOUSE_PRESSED=TRUE;
         MOUSE_X=event->pos().x();
         MOUSE_Y=event->pos().y();
         if(MOUSE_Y<36)
             WINDOW_DRAGGED=TRUE;
    }else if(event->button() == Qt::MidButton){
        WHEEL_PRESSED=TRUE;
        MOUSE_X=event->pos().x();
        MOUSE_Y=event->pos().y();
    }else if(event->button() == Qt::RightButton){
        RIGHT_PRESSED=TRUE;
        MOUSE_X=event->pos().x();
        MOUSE_Y=event->pos().y();
    }
}
void MainWindow::mouseMoveEvent(QMouseEvent *event){
    if((event->buttons() & Qt::MidButton)||(event->buttons() & Qt::LeftButton))
    {
        MOUSE_MOVE=TRUE;
        MOUSE_X=event->pos().x();
        MOUSE_Y=event->pos().y();
        if(WINDOW_DRAGGED&&!FULL)
            move(QPoint(WINDOW_X+event->globalPos().x()-GLOBAL_X,WINDOW_Y+event->globalPos().y()-GLOBAL_Y));
    }
    else{
        MOUSE_X=event->pos().x();
        MOUSE_Y=event->pos().y();
    }
    // qDebug()<<event->pos().x()<<" "<<event->pos().y();
}
void MainWindow::mouseReleaseEvent(QMouseEvent *event){
    if(TO_GET_FILENAME){
        if(filekind==0)
            filename=QFileDialog::getOpenFileName(this,tr("文件："),".",tr("文本文件(*txt)"));
        else if(filekind==1)
            filename=QFileDialog::getSaveFileName(this,tr("文件："),".",tr("文本文件(*txt)"));
        TO_GET_FILENAME=FALSE;
    }
    MOUSE_RELEASED=TRUE;
    WINDOW_DRAGGED=FALSE;
}
void MainWindow::mouseDoubleClickEvent(QMouseEvent * event){
    MOUSE_DOUBLECLICK=TRUE;
    MOUSE_X=event->pos().x();
    MOUSE_Y=event->pos().y();
    if(MOUSE_Y<37){
        if(!FULL){
            move(0,0);
            resize(QSize(QApplication::desktop()->width(),QApplication::desktop()->height()+1));
            FULL=TRUE;
        }else{
            move(QApplication::desktop()->width()/2-400,QApplication::desktop()->height()/2-300);
            resize(800,600);
            FULL=FALSE;
        }
    }
}
void MainWindow::wheelEvent(QWheelEvent *event) {
    int numDegrees = event->delta() / 8;//滚动的角度，*8就是鼠标滚动的距离
    int numSteps = numDegrees /7;//滚动的步数，*15就是鼠标滚动的角度
    if (event->orientation() == Qt::Horizontal) {

     } else {
        if(numSteps>0){
            if ( QApplication::keyboardModifiers () == Qt::ControlModifier)
                CTRL_ROOLUP=TRUE;
            else
                KEY_PRESSED=TRUE;
            KEY_NUM=-7;
            ROOL_NUM=numSteps;
        }else{
            if ( QApplication::keyboardModifiers () == Qt::ControlModifier)
                CTRL_ROOLDOWN=TRUE;
            else
                KEY_PRESSED=TRUE;
            KEY_NUM=-8;
            ROOL_NUM=-numSteps;
        }
     }
    event->accept();      //接收该事件
 }

void MainWindow::slot1(QString q)
{
     char *str;
     int i;
     QByteArray ba = q.toLocal8Bit();
     str=ba.data();
     if(str[0]){
        for(i=0;str[i]!=0&&i<127;i++)
            string[i]=str[i];
        string[i]=0;
        ui->lineEdit->clear();
        KEY_PRESSED=TRUE;
        KEY_NUM=1;
     }
}

void MainWindow::dragEnterEvent(QDragEnterEvent *e)
{
     e->acceptProposedAction();
}

void MainWindow::dropEvent(QDropEvent *e)
{
    char *str;
     QList<QUrl> urls(e->mimeData()->urls());
     if(urls.isEmpty())
         return;
     QString filePath = urls.first().toLocalFile();
     if(filePath.isEmpty())
         return;
     QByteArray ba = filePath.toLocal8Bit();
     str=ba.data();
     if(str[0]){
         int i;
         for(i=0;str[i]!=0&&i<127;i++)
            file[i]=str[i];
         file[i]=0;
     }
     FILE_DRAGGED=TRUE;
     qDebug()<<file;
}
void MainWindow::write_to_clickboard(char* str){
    board=str;
}
QString MainWindow::get_name(int kind){
    filekind=kind;
    TO_GET_FILENAME=TRUE;
    qDebug()<<"000";
    while(TO_GET_FILENAME){Sleep(1);}
    qDebug()<<"111";
    return filename;
}
